package com.znf4.controller.good;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.znf4.base.BaseReturnModel;
import com.znf4.constants.CommonConst;
import com.znf4.good.enums.GoodStatusEnum;
import com.znf4.good.enums.GoodTypeEnum;
import com.znf4.enums.OperationStatusEnum;
import com.znf4.good.crops.entity.Crops;
import com.znf4.good.crops.server.CropsServer;
import com.znf4.id.IdGenerator;
import com.znf4.utils.OSSFile;

/**
 * 商品信息
 */
@RestController
@RequestMapping("/good/crops")
public class CropsGoodController {
	
	private static final Logger LOG = LoggerFactory.getLogger(CropsGoodController.class);
	
	@Autowired
	private CropsServer cropsServer ;
	
	@Autowired
	private IdGenerator idGenerator ;
	

	/**
	 * 函数功能说明 ： 添加农作物商品
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/add", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> add(Crops crops  , @RequestParam(value = "file", required = false)List<MultipartFile> files) {
		
		try {
			crops.setId(idGenerator.getGoodNameNumber());
			String fileName = CommonConst.CROPS_GOOD_IMG_NUMBER + crops.getId();
			// 文件上传OSS
			for (MultipartFile multipartFile : files) {
				String orignalFileName = multipartFile.getOriginalFilename();
				if (multipartFile != null && orignalFileName != null && orignalFileName.length() > 0) {
					crops.setImg(OSSFile.upFile(multipartFile, fileName));
					LOG.info("农作物商品上传图片到oss.....并设置数据库参数。");
				}
			}
			crops.setStatus(GoodStatusEnum.FROZEN.getDesc()); // 状态
			crops.setType(GoodTypeEnum.getEnum("RICE").getDesc());
			cropsServer.addCropsGood(crops);
		} catch (Exception e) {
			LOG.info("添加农作物商品信息异常 :" ,e.getMessage());
			return BaseReturnModel.getMapModel("faild", "", OperationStatusEnum.ERROR.getDesc());
		}
		return BaseReturnModel.getMapModel("success", "操作完成", "");
	}

	
	
	/**
	 * 函数功能说明 ： 修改农作物商品
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/update", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> update(Crops crops , @RequestParam(value = "file", required = false)List<MultipartFile> files) {

		try {
			String fileName = CommonConst.CROPS_GOOD_IMG_NUMBER + crops.getId();
			// 文件上传OSS
			for (MultipartFile multipartFile : files) {
				String orignalFileName = multipartFile.getOriginalFilename();
				if (multipartFile != null && orignalFileName != null && orignalFileName.length() > 0) {
					crops.setImg(OSSFile.upFile(multipartFile, fileName));
					LOG.info("农作物商品上传图片到oss.....并设置数据库参数。");
				}
			}
			cropsServer.updateCropsGood(crops);
		} catch (Exception e) {
			LOG.info("修改农作物商品信息异常 :", e.getMessage());
			return BaseReturnModel.getMapModel("faild", "", OperationStatusEnum.ERROR.getDesc());
		}
		return BaseReturnModel.getMapModel("success", "操作完成", "");
	}

	/**
	 * 函数功能说明 ： 删除农作物商品
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/delOne", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> delOne(Crops crops ) {
		
		try {
			cropsServer.delOneCropsGood(crops);
			// 删除文件 OSS
			OSSFile.deleteFile(crops.getImg());
		} catch (Exception e) {
			LOG.info("删除单个农作物商品信息异常 :", e.getMessage());
			return BaseReturnModel.getMapModel("faild", "", OperationStatusEnum.ERROR.getDesc());
		}
		return BaseReturnModel.getMapModel("success", "操作完成", "");
	}

	/**
	 * 函数功能说明 ： 删除多条农作物商品
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/delAll", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> delAll(String[] ids) {
		try {
			List<Crops> lCrops = new ArrayList<>();
			for (int i = 0; i < ids.length; i++) {
				Crops crop = new Crops();
				crop.setId(ids[i]);
				lCrops.add(crop);
				// 删除文件
				Crops delCrop = cropsServer.queryOne(crop);
				OSSFile.deleteFile(delCrop.getImg());
			}
			cropsServer.delAllCropsGood(lCrops);
		} catch (Exception e) {
			LOG.info("删除多个农作物商品信息异常 :", e.getMessage());
			return BaseReturnModel.getMapModel("faild", "", OperationStatusEnum.ERROR.getDesc());
		}
		return BaseReturnModel.getMapModel("success", "操作完成", "");
	}
	

	/**
	 * 函数功能说明 ： 获取所有商城商品
	 *
	 * @参数： @return @return String @throws
	 */
	@RequestMapping(value = "/queryAll", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> queryAll(Crops crops ) {
		List<Crops> list = cropsServer.queryAll(crops);
		return BaseReturnModel.getMapModel("success", list, "");
	}
	
	
	
	
	
	
	
	

}
